import { TabbarPageEnum } from "@/enums/PageEnum";
import { getUrl } from "@/router";
import { QueryType } from "@/router/types";
import { CustomNavigateToOptions } from "@/router/types/nav";
import { PagesType } from "@/router/types/pages";
import { useUserStoreWithOut } from "@/store/modules/user";
import { useTmpiniaStore } from "@/tmui/tool/lib/tmpinia";
import { forIn, isObject } from "lodash-es";

interface IOptions {
  /** 是否禁止复制链接 */
  isDisabledCopyUrl: boolean
}

/**
 * @description 创建一个url参数
 * @date 15/11/2023
 * @param {Recordable} query
 */
export function createShareQueryStr(query: Recordable) {
  // const { shareId } = useUserStoreWithOut()
  const keyStr = 'shareId'
  let queryStr = ''
  forIn(query, (value, key) => {
    if (value && key !== keyStr) queryStr += `${key}=${query[key]}&`
  });
  // queryStr += `${keyStr}=${shareId}`
  return queryStr
}

export function useAppShare(options?: IOptions) {
  const { shareId } = useUserStoreWithOut()
  const store = useTmpiniaStore();

  // const shortLinkQuery = ref<Recordable>()

  // uni.onCopyUrl(() => {
  //   if (options?.isDisabledCopyUrl) {
  //     uni.showModal({
  //       title: '提示',
  //       content: '该页面Sort Link无效, 慎用',
  //       showCancel: false,
  //       success: () => {
  //         uni.setClipboardData({ data: '' })
  //       },
  //     });
  //   }
  //   return { query: createShareQueryStr(shortLinkQuery.value || {}) }
  // });

  // onUnload(() => {
  //   uni.offCopyUrl()
  // })
  // /**
  //  * @description 设置短链自定义参数
  //  * @date 15/11/2023
  //  * @param {Recordable} query
  //  */
  // function setShortLinkQuery(query: Recordable) {
  //   shortLinkQuery.value = query
  // }

  /**
   * @description 重定向分享路径
   * @date 14/11/2023
   * @export
   * @template T
   * @param {CustomNavigateToOptions<T>} op
   */
  function setWxSharePath<T extends PagesType>(op: CustomNavigateToOptions<T>) {
    let { name, options } = op
    if (!isObject(options)) options = {}
    let str = JSON.stringify({ ...options, shareId })
    const path = name // getUrl(name);
    if (!path) {
      throw '【setWxSharePath】==> 请传入正确的路由名';
    }
    const startPage = getUrl(TabbarPageEnum.STARTPAGE, {
      path,
      ...(str && { query: str }),
    });
    return decodeURIComponent(startPage);
  }

  /**
 * @description 设置微信分享朋友圈参数
 * @date 14/11/2023
 * @export
 * @param {QueryType} query
 * @returns {string}
 */
  // function setWxShareTimelineQuery(query: QueryType): string {
  //   return createShareQueryStr({ ...query, themeColor: store.getThemeColor })
  // }




  return {
    // setWxSharePath,
    // setWxShareTimelineQuery,
    // setShortLinkQuery
  }
}